﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrmContacto.Contracts.Services;
using CrmContacto.Contracts.Config;
using System.Configuration;
using Web.Framework.Translate;
using System.Reflection;

namespace CrmContacto.Services.Classes
{
    public class ServiceFactoryInternal
    {
        protected static ServiceFactoryInternal innerInstance = new ServiceFactoryInternal();

        public static ServiceFactoryInternal Instance
        {
            get
            {
                return innerInstance;
            }
        }       

        private Dictionary<int, List<ICustomerServiceHook>> dictHooks = new Dictionary<int, List<ICustomerServiceHook>>();
        public void ClearHookList()
        {
            dictHooks.Clear();
        }

        public List<ICustomerServiceHook> GetCustomerServiceHooks(int empresaId)
        {
            List<ICustomerServiceHook> hookList = new List<ICustomerServiceHook>();
            if (dictHooks.ContainsKey(empresaId))
            {
                hookList = dictHooks[empresaId];
            }
            else
            {
                CustomerServiceHookConfig configHookCollection = ConfigurationManager.GetSection("CustomerServiceHookProvidersSection") as CustomerServiceHookConfig;

                if (configHookCollection != null)
                {
                    foreach (var configItem in configHookCollection.CustomerServiceHooks)
                    {
                        CustomerServiceHookProviderElement providerItem = configItem as CustomerServiceHookProviderElement;

                        if (providerItem != null)
                        {
                            if (!String.IsNullOrEmpty(providerItem.Provider))
                            {
                                if (Convert.ToInt32(providerItem.EmpresaId) == empresaId)
                                {
                                    Assembly assembly = Assembly.LoadFrom(providerItem.AssemblyFile);
                                    Type tipoHook = assembly.GetType(providerItem.Provider);
                                    try
                                    {
                                        ICustomerServiceHook iHook = Activator.CreateInstance(tipoHook) as ICustomerServiceHook;
                                        if (iHook != null)
                                        {
                                            iHook.LoadConfiguration(providerItem.ConfigurationString);
                                            hookList.Add(iHook);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception(LocalizationProvider.Instance["GenericErrorTitle"] + ex.Message + ex.StackTrace.First());
                                    }
                                }
                            }
                        }
                    }

                    dictHooks.Add(empresaId, hookList);
                }
            }

            return hookList;
        }
    }
}
